<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #663399;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            color: white;
        }

        .login-container {
            background-color: rgba(255, 255, 255, 0.2);
            padding: 30px;
            border-radius: 10px;
            width: 350px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }

        fieldset {
            border: 2px solid white;
            border-radius: 5px;
            padding: 20px;
        }

        legend {
            font-size: 1.2em;
            font-weight: bold;
            padding: 0 10px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }

        .error {
            color: #ffcc00;
            font-size: 0.8em;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <fieldset>
            <legend>用户登录</legend>
            <form id="loginForm">
                <div class="form-group">
                    <label for="username">用户名:</label>
                    <input type="text" id="username" name="username">
                    <div id="usernameError" class="error"></div>
                </div>
                <div class="form-group">
                    <label for="password">密码:</label>
                    <input type="password" id="password" name="password">
                    <div id="passwordError" class="error"></div>
                </div>
                <button type="submit">登录</button>
            </form>
        </fieldset>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();

            // 获取输入值
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();

            // 重置错误提示
            document.getElementById('usernameError').style.display = 'none';
            document.getElementById('passwordError').style.display = 'none';

            let isValid = true;

            // 验证用户名
            if (!username) {
                showError('usernameError', '用户名不能为空');
                document.getElementById('username').focus();
                isValid = false;
            } else if (username.length < 8 || username.length > 20) {
                showError('usernameError', '用户名长度必须为8-20个字符');
                document.getElementById('username').focus();
                isValid = false;
            }

            // 验证密码
            if (!password) {
                showError('passwordError', '密码不能为空');
                // 只有当用户名验证通过时才聚焦密码框
                if (isValid) document.getElementById('password').focus();
                isValid = false;
            } else if (password.length < 8 || password.length > 20) {
                showError('passwordError', '密码长度必须为8-20个字符');
                // 只有当用户名验证通过时才聚焦密码框
                if (isValid) document.getElementById('password').focus();
                isValid = false;
            }

            // 如果验证通过，可以提交表单
            if (isValid) {
                alert('登录信息验证通过！');
                // 这里可以添加实际的表单提交代码
                // this.submit();
            }
        });

        function showError(elementId, message) {
            const errorElement = document.getElementById(elementId);
            errorElement.textContent = message;
            errorElement.style.display = 'block';
        }
    </script>
</body>
</html>
